<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Wallet;
use App\Models\Currency;
use App\Library\BTC\BitcoinFunction;

/**
 * 创建系统BTC钱包
 */
class CreateBtcWallet extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'system:create_btc_wallet';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //btc钱包
        $cid = Currency::where('name_en', 'BTC')->value('id');
        if ($cid) {
            $unlocks = Wallet::where(['status' => 0, 'cid' => $cid])->count();
            //未锁定钱包少于3个则创建新的系统钱包
            if ($unlocks < 3) {
                $btc = new BitcoinFunction('btc');
                $res = $btc->createWalletAddress();
                if ($res) {
                    Wallet::create(
                        [
                            'cid' => $cid,
                            'address' => $res,
                            'balance' => 0,
                            'block_balance' => 0,
                            'grand_recharge' => 0,
                            'grand_withdraw' => 0,
                            'grand_proc' => 0,
                            'status' => 0,
                        ]
                    );
                }
            }            
        }
    }
}
